package com.mszlu.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.blog.dao.mapper.AdminMapper;
import com.mszlu.blog.dao.pojo.Admin;
import com.mszlu.blog.service.AdminService;
import com.mszlu.blog.utils.EmptyUtils;
import com.mszlu.blog.vo.ErrorCode;
import com.mszlu.blog.vo.PageResult;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.params.UserPageParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Package: com.learn.blog.admin.service
 * @ClassName: AdminService
 * @Author: 陈威
 * @CreateTime: 2021/9/14 21:37
 * @Description:
 */
@SuppressWarnings("all")
@Service
public class AdminServiceImpl implements AdminService {
    //加盐
    private static final String slat = "mszlu!@#";
    @Autowired
    AdminMapper adminMapper;


    @Override
    public Result addAdmin(Admin admin) {
        if (EmptyUtils.isEmpty(admin)) {
            return Result.fail(999, "参数为空");
        }
        String password = admin.getPassword();
        admin.setPassword(DigestUtils.md5Hex(password + slat));
        admin.setAvatar("http://localhost:8080/static/img/logo.b3a48c0.png");
        adminMapper.insert(admin);
        return Result.success(null);
    }

    @Override
    public Result updateAdmin(Admin admin) {
        if (EmptyUtils.isEmpty(admin)) {
            return Result.fail(999, "参数为空");
        }
        String password = admin.getPassword();
        admin.setPassword(DigestUtils.md5Hex(password + slat));
        adminMapper.updateById(admin);
        return Result.success(null);
    }

    @Override
    public Result deleteAdmin(Long id) {
        if (EmptyUtils.isEmpty(id)) {
            return Result.fail(999, "参数为空");
        }
        adminMapper.deleteById(id);
        return Result.success(null);
    }

    //查询用户分页结果
    public Result adminList(UserPageParam pageParam) {
        if (EmptyUtils.isEmpty(pageParam)){
            return new Result(999, ErrorCode.PARAM_NULL.getMsg());
        }
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        if(EmptyUtils.isNotEmpty(pageParam.getId())){
            queryWrapper.eq(Admin::getId,pageParam.getId());
        }
        if (StringUtils.isNotBlank(pageParam.getAccount())) {
            queryWrapper.like(Admin::getAccount, pageParam.getAccount());
        }

        Page<Admin> adminPage = new Page<>(pageParam.getPageNo(), pageParam.getPageSize());
        Page<Admin> selectPage = adminMapper.selectPage(adminPage, queryWrapper);
        PageResult<Admin> pageResult = new PageResult<>();
        pageResult.setList(selectPage.getRecords());
        pageResult.setTotal(selectPage.getTotal());
        return Result.success(pageResult);
    }

    public Admin findAdminByUserName(String userName) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getAccount, userName);
        queryWrapper.last("limit 1");
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public Result resetPwd(Admin admin) {
        if (EmptyUtils.isEmpty(admin)) {
            return Result.fail(999, "参数为空");
        }
        String password = "123";
        admin.setPassword(DigestUtils.md5Hex(password + slat));
        adminMapper.updateById(admin);
        return Result.success(null);
    }
}